package com.ethanzzh.security.core.ethanzzhdemo.controller;



import com.ethanzzh.security.core.ethanzzhdemo.dto.User;
import com.ethanzzh.security.core.ethanzzhdemo.dto.UserQueryCondition;
import com.fasterxml.jackson.annotation.JsonView;
import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.springframework.data.web.PageableDefault;
import org.springframework.security.core.Authentication;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.data.domain.Pageable;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {


    @GetMapping("/me")
    public Object getCurrentUser(Authentication authentication) {
//        return SecurityContextHolder.getContext().getAuthentication();
        return authentication;
    }



    @DeleteMapping("/{id:\\d+}")
    public void delete(@PathVariable String id){
        System.out.println(id);
    }


    @PutMapping("/{id:\\d+}")
    public User update(@Valid @RequestBody User user, BindingResult errors) {
        if (errors.hasErrors()) {
            errors.getAllErrors().stream().forEach(
                    error -> {
//                        FieldError fieldError = (FieldError) error;
//                        String message = fieldError.getField()+ " "+ error.getDefaultMessage();
                        System.out.println(error.getDefaultMessage());
                    });
        }

        System.out.println(user.getId());
        System.out.println(user.getPassword());
        System.out.println(user.getUsername());
        System.out.println(user.getBirthday());

        user.setId("1");
        return user;
    }

    @PostMapping
    public User create(@Valid @RequestBody User user, BindingResult errors) {
        if (errors.hasErrors()) {
            errors.getAllErrors().stream().forEach(e -> System.out.println(e.getDefaultMessage()));
        }

        System.out.println(user.getId());
        System.out.println(user.getPassword());
        System.out.println(user.getUsername());
        System.out.println(user.getBirthday());

        user.setId("1");
        return user;
    }


    @JsonView(User.UserSimpleView.class)
//    @RequestMapping(value = "/user", method = RequestMethod.GET)
    @GetMapping()
    public List<User> query(UserQueryCondition userQueryCondition, @PageableDefault(page = 2, size = 17, sort = "username,asc") Pageable pageable) {

        System.out.println(ReflectionToStringBuilder.toString(userQueryCondition, ToStringStyle.MULTI_LINE_STYLE));

        System.out.println(pageable.getPageSize());
        System.out.println(pageable.getPageNumber());
        System.out.println(pageable.getSort());


        List<User> userList = new ArrayList<>();

        userList.add(new User());
        userList.add(new User());
        userList.add(new User());

        return userList;
    }

    //id 正则表达式  表示只能接收数字
    //@PathVariable 用来指定url路径中的变量
//    @RequestMapping(value = "/user/{id:\\d+}", method = RequestMethod.GET)
    @JsonView(User.UserDatailView.class)
    @GetMapping("/{id:\\d+}")
    public User getInfo(@PathVariable(name = "id") String id) {

        System.out.println("进入getInfo服务");
        User user = new User();
        user.setUsername("tom");
        return user;
    }
}
